<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>分布式ID | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C02-Database/DatabaseOptimize.html" />
    
    
    <link rel="prev" href="../C02-Database/DatabaseTheory.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="2.6"
        data-chapter-title="分布式ID"
        data-filepath="C02-Database/DistributedID.md"
        data-basepath=".."
        data-revision="Mon May 27 2019 03:35:45 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Introduction/index.html">
            
                
                    <a href="../C01-Introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Introduction/AboutToutiaoWebVM.html">
            
                
                    <a href="../C01-Introduction/AboutToutiaoWebVM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        ToutiaoWeb虚拟机使用说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Introduction/PycharmRemoteDevelop.html">
            
                
                    <a href="../C01-Introduction/PycharmRemoteDevelop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Pycharm远程开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Introduction/AboutProject.html">
            
                
                    <a href="../C01-Introduction/AboutProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        产品与开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Database/index.html">
            
                
                    <a href="../C02-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Database/DatabaseDesign.html">
            
                
                    <a href="../C02-Database/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Database/ORM.html">
            
                
                    <a href="../C02-Database/ORM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        理解ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Database/SQLAlchemyMapping.html">
            
                
                    <a href="../C02-Database/SQLAlchemyMapping.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        SQLAlchemy映射构建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Database/SQLAlchemyOperations.html">
            
                
                    <a href="../C02-Database/SQLAlchemyOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        SQLAlchemy操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Database/DatabaseTheory.html">
            
                
                    <a href="../C02-Database/DatabaseTheory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        数据库理论
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.6" data-path="C02-Database/DistributedID.html">
            
                
                    <a href="../C02-Database/DistributedID.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        分布式ID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Database/DatabaseOptimize.html">
            
                
                    <a href="../C02-Database/DatabaseOptimize.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        数据库优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Database/Redis.html">
            
                
                    <a href="../C02-Database/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Gitflow/index.html">
            
                
                    <a href="../C03-Gitflow/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Git工作流
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Gitflow/Gitflow.html">
            
                
                    <a href="../C03-Gitflow/Gitflow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Gitflow
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Gitflow/AboutGit.html">
            
                
                    <a href="../C03-Gitflow/AboutGit.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Git总结
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Gitflow/ProjectDirectory.html">
            
                
                    <a href="../C03-Gitflow/ProjectDirectory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        工程目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Gitflow/DebugMethod.html">
            
                
                    <a href="../C03-Gitflow/DebugMethod.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        调试方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-JWT/index.html">
            
                
                    <a href="../C04-JWT/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-JWT/JWT.html">
            
                
                    <a href="../C04-JWT/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        JWT &amp; JWS &amp; JWE
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-JWT/PythonLib.html">
            
                
                    <a href="../C04-JWT/PythonLib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        PythonLib
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-JWT/Implement.html">
            
                
                    <a href="../C04-JWT/Implement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        实施方案
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-JWT/Forbbiden.html">
            
                
                    <a href="../C04-JWT/Forbbiden.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        禁用问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-OSS/index.html">
            
                
                    <a href="../C05-OSS/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象存储
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-OSS/Qiniu.html">
            
                
                    <a href="../C05-OSS/Qiniu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        七牛云存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-OSS/CDN.html">
            
                
                    <a href="../C05-OSS/CDN.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        CDN
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Cache/index.html">
            
                
                    <a href="../C06-Cache/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        缓存
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Cache/CacheArchitecture.html">
            
                
                    <a href="../C06-Cache/CacheArchitecture.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        缓存的架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Cache/CacheData.html">
            
                
                    <a href="../C06-Cache/CacheData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Cache/CacheTTLAndEviction.html">
            
                
                    <a href="../C06-Cache/CacheTTLAndEviction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        缓存的有效期与淘汰策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C06-Cache/CachePatterns.html">
            
                
                    <a href="../C06-Cache/CachePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        缓存模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C06-Cache/CacheProblems.html">
            
                
                    <a href="../C06-Cache/CacheProblems.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        缓存问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C06-Cache/ProjectCacheAndStorageDesign.html">
            
                
                    <a href="../C06-Cache/ProjectCacheAndStorageDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        项目缓存与Redis持久存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="C06-Cache/ProjectCacheImplement.html">
            
                
                    <a href="../C06-Cache/ProjectCacheImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        项目缓存实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="C06-Cache/ProjectRedisStorageImplement.html">
            
                
                    <a href="../C06-Cache/ProjectRedisStorageImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        项目Redis持久存储实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-APScheduler/index.html">
            
                
                    <a href="../C07-APScheduler/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        APScheduler
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C07-APScheduler/APSchedulerTutorial.html">
            
                
                    <a href="../C07-APScheduler/APSchedulerTutorial.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        APScheduler使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C07-APScheduler/FixStatistics.html">
            
                
                    <a href="../C07-APScheduler/FixStatistics.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定时修正统计数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-RPC/index.html">
            
                
                    <a href="../C08-RPC/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        RPC
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C08-RPC/AboutRPC.html">
            
                
                    <a href="../C08-RPC/AboutRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        RPC简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C08-RPC/gRPC.html">
            
                
                    <a href="../C08-RPC/gRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        gRPC
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C08-RPC/ProtocolBuffers.html">
            
                
                    <a href="../C08-RPC/ProtocolBuffers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        Protocol Buffers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C08-RPC/RecoSysIDL.html">
            
                
                    <a href="../C08-RPC/RecoSysIDL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        推荐系统接口定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C08-RPC/Server.html">
            
                
                    <a href="../C08-RPC/Server.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="C08-RPC/Client.html">
            
                
                    <a href="../C08-RPC/Client.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="C08-RPC/ToutiaoAPIViewImplement.html">
            
                
                    <a href="../C08-RPC/ToutiaoAPIViewImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        头条首页新闻推荐接口编写
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-IM/index.html">
            
                
                    <a href="../C09-IM/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        即时通信
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-IM/AboutIM.html">
            
                
                    <a href="../C09-IM/AboutIM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        即时通信简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C09-IM/WebSocket.html">
            
                
                    <a href="../C09-IM/WebSocket.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C09-IM/SocketIO.html">
            
                
                    <a href="../C09-IM/SocketIO.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        Socket.IO
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C09-IM/Chat.html">
            
                
                    <a href="../C09-IM/Chat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        头条聊天服务实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C09-IM/MessagePush.html">
            
                
                    <a href="../C09-IM/MessagePush.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        头条在线消息推送实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Elasticsearch/index.html">
            
                
                    <a href="../C10-Elasticsearch/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Elasticsearch
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="C10-Elasticsearch/Introduction.html">
            
                
                    <a href="../C10-Elasticsearch/Introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        简介与原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="C10-Elasticsearch/ConceptAndCluster.html">
            
                
                    <a href="../C10-Elasticsearch/ConceptAndCluster.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        概念与集群
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="C10-Elasticsearch/AnalysisIK.html">
            
                
                    <a href="../C10-Elasticsearch/AnalysisIK.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        IK中文分析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="C10-Elasticsearch/IndexAndType.html">
            
                
                    <a href="../C10-Elasticsearch/IndexAndType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        索引与类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="C10-Elasticsearch/Document.html">
            
                
                    <a href="../C10-Elasticsearch/Document.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="C10-Elasticsearch/Logstash.html">
            
                
                    <a href="../C10-Elasticsearch/Logstash.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        Logstash导入数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="C10-Elasticsearch/Query.html">
            
                
                    <a href="../C10-Elasticsearch/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="C10-Elasticsearch/ToutiaoSearchImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSearchImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        头条全文检索实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="C10-Elasticsearch/Suggest.html">
            
                
                    <a href="../C10-Elasticsearch/Suggest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        suggest查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="C10-Elasticsearch/ToutiaoSuggestImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSuggestImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        头条suggest查询实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="C11-Unittest.html">
            
                
                    <a href="../C11-Unittest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="C12-Deployment/index.html">
            
                
                    <a href="../C12-Deployment/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        部署相关
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="C12-Deployment/Gunicorn.html">
            
                
                    <a href="../C12-Deployment/Gunicorn.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        Gunicorn
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="C12-Deployment/Supervisor.html">
            
                
                    <a href="../C12-Deployment/Supervisor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        Supervisor
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5206;&#x5E03;&#x5F0F;id">&#x5206;&#x5E03;&#x5F0F;ID</h1>
<h2 id="1-&#x65B9;&#x6848;&#x9009;&#x62E9;">1 &#x65B9;&#x6848;&#x9009;&#x62E9;</h2>
<ul>
<li><p><strong>UUID</strong></p>
<p>UUID&#x662F;&#x901A;&#x7528;&#x552F;&#x4E00;&#x8BC6;&#x522B;&#x7801;&#xFF08;Universally Unique Identifier)&#x7684;&#x7F29;&#x5199;&#xFF0C;&#x5F00;&#x653E;&#x8F6F;&#x4EF6;&#x57FA;&#x91D1;&#x4F1A;(OSF)&#x89C4;&#x8303;&#x5B9A;&#x4E49;&#x4E86;&#x5305;&#x62EC;&#x7F51;&#x5361;MAC&#x5730;&#x5740;&#x3001;&#x65F6;&#x95F4;&#x6233;&#x3001;&#x540D;&#x5B57;&#x7A7A;&#x95F4;&#xFF08;Namespace&#xFF09;&#x3001;&#x968F;&#x673A;&#x6216;&#x4F2A;&#x968F;&#x673A;&#x6570;&#x3001;&#x65F6;&#x5E8F;&#x7B49;&#x5143;&#x7D20;&#x3002;&#x5229;&#x7528;&#x8FD9;&#x4E9B;&#x5143;&#x7D20;&#x6765;&#x751F;&#x6210;UUID&#x3002;</p>
<p>UUID&#x662F;&#x7531;128&#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x7EC4;&#x6210;&#xFF0C;&#x4E00;&#x822C;&#x8F6C;&#x6362;&#x6210;&#x5341;&#x516D;&#x8FDB;&#x5236;&#xFF0C;&#x7136;&#x540E;&#x7528;String&#x8868;&#x793A;&#x3002;</p>
<blockquote>
<p>550e8400-e29b-41d4-a716-446655440000</p>
</blockquote>
<p>UUID&#x7684;&#x4F18;&#x70B9;:</p>
<ul>
<li>&#x901A;&#x8FC7;&#x672C;&#x5730;&#x751F;&#x6210;&#xFF0C;&#x6CA1;&#x6709;&#x7ECF;&#x8FC7;&#x7F51;&#x7EDC;I/O&#xFF0C;&#x6027;&#x80FD;&#x8F83;&#x5FEB;</li>
<li>&#x65E0;&#x5E8F;&#xFF0C;&#x65E0;&#x6CD5;&#x9884;&#x6D4B;&#x4ED6;&#x7684;&#x751F;&#x6210;&#x987A;&#x5E8F;&#x3002;(&#x5F53;&#x7136;&#x8FD9;&#x4E2A;&#x4E5F;&#x662F;&#x4ED6;&#x7684;&#x7F3A;&#x70B9;&#x4E4B;&#x4E00;)</li>
</ul>
<p>UUID&#x7684;&#x7F3A;&#x70B9;:</p>
<ul>
<li>128&#x4F4D;&#x4E8C;&#x8FDB;&#x5236;&#x4E00;&#x822C;&#x8F6C;&#x6362;&#x6210;36&#x4F4D;&#x7684;16&#x8FDB;&#x5236;&#xFF0C;&#x592A;&#x957F;&#x4E86;&#x53EA;&#x80FD;&#x7528;String&#x5B58;&#x50A8;&#xFF0C;&#x7A7A;&#x95F4;&#x5360;&#x7528;&#x8F83;&#x591A;&#x3002;</li>
<li>&#x4E0D;&#x80FD;&#x751F;&#x6210;&#x9012;&#x589E;&#x6709;&#x5E8F;&#x7684;&#x6570;&#x5B57;</li>
</ul>
</li>
<li><p><strong>&#x6570;&#x636E;&#x5E93;&#x4E3B;&#x952E;&#x81EA;&#x589E;</strong></p>
<p>&#x5927;&#x5BB6;&#x5BF9;&#x4E8E;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x6700;&#x5BB9;&#x6613;&#x60F3;&#x5230;&#x7684;&#x5C31;&#x662F;&#x4E3B;&#x952E;&#x81EA;&#x589E;&#xFF0C;&#x8FD9;&#x4E2A;&#x4E5F;&#x662F;&#x6211;&#x4EEC;&#x6700;&#x5E38;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F8B;&#x5982;&#x6211;&#x4EEC;&#x6709;&#x4E2A;&#x8BA2;&#x5355;&#x670D;&#x52A1;&#xFF0C;&#x90A3;&#x4E48;&#x628A;&#x8BA2;&#x5355;id&#x8BBE;&#x7F6E;&#x4E3A;&#x4E3B;&#x952E;&#x81EA;&#x589E;&#x5373;&#x53EF;&#x3002;</p>
<ul>
<li><p>&#x5355;&#x72EC;&#x6570;&#x636E;&#x5E93; &#x8BB0;&#x5F55;&#x4E3B;&#x952E;&#x503C;</p>
</li>
<li><p>&#x4E1A;&#x52A1;&#x6570;&#x636E;&#x5E93;&#x5206;&#x522B;&#x8BBE;&#x7F6E;&#x4E0D;&#x540C;&#x7684;&#x81EA;&#x589E;&#x8D77;&#x59CB;&#x503C;&#x548C;&#x56FA;&#x5B9A;&#x6B65;&#x957F;&#xFF0C;&#x5982;</p>
<pre><code class="lang-shell">&#x7B2C;&#x4E00;&#x53F0; start 1  step 9 
&#x7B2C;&#x4E8C;&#x53F0; start 2  step 9 
&#x7B2C;&#x4E09;&#x53F0; start 3  step 9
</code></pre>
</li>
</ul>
<p>&#x4F18;&#x70B9;:</p>
<ul>
<li>&#x7B80;&#x5355;&#x65B9;&#x4FBF;&#xFF0C;&#x6709;&#x5E8F;&#x9012;&#x589E;&#xFF0C;&#x65B9;&#x4FBF;&#x6392;&#x5E8F;&#x548C;&#x5206;&#x9875;</li>
</ul>
<p>&#x7F3A;&#x70B9;:</p>
<ul>
<li>&#x5206;&#x5E93;&#x5206;&#x8868;&#x4F1A;&#x5E26;&#x6765;&#x95EE;&#x9898;&#xFF0C;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x6539;&#x9020;&#x3002;</li>
<li>&#x5E76;&#x53D1;&#x6027;&#x80FD;&#x4E0D;&#x9AD8;&#xFF0C;&#x53D7;&#x9650;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x6027;&#x80FD;&#x3002;</li>
<li>&#x7B80;&#x5355;&#x9012;&#x589E;&#x5BB9;&#x6613;&#x88AB;&#x5176;&#x4ED6;&#x4EBA;&#x731C;&#x6D4B;&#x5229;&#x7528;&#xFF0C;&#x6BD4;&#x5982;&#x4F60;&#x6709;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x670D;&#x52A1;&#x7528;&#x7684;&#x9012;&#x589E;&#xFF0C;&#x90A3;&#x4E48;&#x5176;&#x4ED6;&#x4EBA;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x5206;&#x6790;&#x6CE8;&#x518C;&#x7684;&#x7528;&#x6237;ID&#x6765;&#x5F97;&#x5230;&#x5F53;&#x5929;&#x4F60;&#x7684;&#x670D;&#x52A1;&#x6709;&#x591A;&#x5C11;&#x4EBA;&#x6CE8;&#x518C;&#xFF0C;&#x4ECE;&#x800C;&#x5C31;&#x80FD;&#x731C;&#x6D4B;&#x51FA;&#x4F60;&#x8FD9;&#x4E2A;&#x670D;&#x52A1;&#x5F53;&#x524D;&#x7684;&#x4E00;&#x4E2A;&#x5927;&#x6982;&#x72B6;&#x51B5;&#x3002;</li>
<li>&#x6570;&#x636E;&#x5E93;&#x5B95;&#x673A;&#x670D;&#x52A1;&#x4E0D;&#x53EF;&#x7528;&#x3002;</li>
</ul>
</li>
<li><p><strong>Redis</strong></p>
<p>&#x719F;&#x6089;Redis&#x7684;&#x540C;&#x5B66;&#xFF0C;&#x5E94;&#x8BE5;&#x77E5;&#x9053;&#x5728;Redis&#x4E2D;&#x6709;&#x4E24;&#x4E2A;&#x547D;&#x4EE4;Incr&#xFF0C;IncrBy,&#x56E0;&#x4E3A;Redis&#x662F;&#x5355;&#x7EBF;&#x7A0B;&#x7684;&#x6240;&#x4EE5;&#x80FD;&#x4FDD;&#x8BC1;&#x539F;&#x5B50;&#x6027;&#x3002;</p>
<p>&#x4F18;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x6027;&#x80FD;&#x6BD4;&#x6570;&#x636E;&#x5E93;&#x597D;&#xFF0C;&#x80FD;&#x6EE1;&#x8DB3;&#x6709;&#x5E8F;&#x9012;&#x589E;&#x3002;</li>
</ul>
<p>&#x7F3A;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x7531;&#x4E8E;redis&#x662F;&#x5185;&#x5B58;&#x7684;KV&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5373;&#x4F7F;&#x6709;AOF&#x548C;RDB&#xFF0C;&#x4F46;&#x662F;&#x4F9D;&#x7136;&#x4F1A;&#x5B58;&#x5728;&#x6570;&#x636E;&#x4E22;&#x5931;&#xFF0C;&#x6709;&#x53EF;&#x80FD;&#x4F1A;&#x9020;&#x6210;ID&#x91CD;&#x590D;&#x3002;</li>
<li>&#x4F9D;&#x8D56;&#x4E8E;redis&#xFF0C;redis&#x8981;&#x662F;&#x4E0D;&#x7A33;&#x5B9A;&#xFF0C;&#x4F1A;&#x5F71;&#x54CD;ID&#x751F;&#x6210;&#x3002;</li>
</ul>
</li>
<li><p><strong>&#x96EA;&#x82B1;&#x7B97;&#x6CD5;-Snowflake</strong></p>
<p>Snowflake&#x662F;Twitter&#x63D0;&#x51FA;&#x6765;&#x7684;&#x4E00;&#x4E2A;&#x7B97;&#x6CD5;&#xFF0C;&#x5176;&#x76EE;&#x7684;&#x662F;&#x751F;&#x6210;&#x4E00;&#x4E2A;64bit&#x7684;&#x6574;&#x6570;:</p>
<p><img src="../images/snowflake.png" alt="snowflake"></p>
<ul>
<li>1bit:&#x4E00;&#x822C;&#x662F;&#x7B26;&#x53F7;&#x4F4D;&#xFF0C;&#x4E0D;&#x505A;&#x5904;&#x7406;</li>
<li>41bit:&#x7528;&#x6765;&#x8BB0;&#x5F55;&#x65F6;&#x95F4;&#x6233;&#xFF0C;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x8BB0;&#x5F55;69&#x5E74;&#xFF0C;&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x597D;&#x8D77;&#x59CB;&#x65F6;&#x95F4;&#x6BD4;&#x5982;&#x4ECA;&#x5E74;&#x662F;2018&#x5E74;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x7528;&#x5230;2089&#x5E74;&#xFF0C;&#x5230;&#x65F6;&#x5019;&#x600E;&#x4E48;&#x529E;&#xFF1F;&#x8981;&#x662F;&#x8FD9;&#x4E2A;&#x7CFB;&#x7EDF;&#x80FD;&#x7528;69&#x5E74;&#xFF0C;&#x6211;&#x76F8;&#x4FE1;&#x8FD9;&#x4E2A;&#x7CFB;&#x7EDF;&#x65E9;&#x90FD;&#x91CD;&#x6784;&#x4E86;&#x597D;&#x591A;&#x6B21;&#x4E86;&#x3002;</li>
<li>10bit:10bit&#x7528;&#x6765;&#x8BB0;&#x5F55;&#x673A;&#x5668;ID&#xFF0C;&#x603B;&#x5171;&#x53EF;&#x4EE5;&#x8BB0;&#x5F55;1024&#x53F0;&#x673A;&#x5668;&#xFF0C;&#x4E00;&#x822C;&#x7528;&#x524D;5&#x4F4D;&#x4EE3;&#x8868;&#x6570;&#x636E;&#x4E2D;&#x5FC3;&#xFF0C;&#x540E;&#x9762;5&#x4F4D;&#x662F;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x4E2D;&#x5FC3;&#x7684;&#x673A;&#x5668;ID</li>
<li>12bit:&#x5FAA;&#x73AF;&#x4F4D;&#xFF0C;&#x7528;&#x6765;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x6BEB;&#x79D2;&#x4E4B;&#x5185;&#x4EA7;&#x751F;&#x4E0D;&#x540C;&#x7684;ID&#xFF0C;12&#x4F4D;&#x53EF;&#x4EE5;&#x6700;&#x591A;&#x8BB0;&#x5F55;4095&#x4E2A;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x673A;&#x5668;&#x540C;&#x4E00;&#x6BEB;&#x79D2;&#x6700;&#x591A;&#x8BB0;&#x5F55;4095&#x4E2A;&#xFF0C;&#x591A;&#x4F59;&#x7684;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#x4E0B;&#x6BEB;&#x79D2;&#x3002;</li>
</ul>
<p>&#x4E0A;&#x9762;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x5C06;64bit&#x5212;&#x5206;&#x7684;&#x6807;&#x51C6;&#xFF0C;&#x5F53;&#x7136;&#x4E5F;&#x4E0D;&#x4E00;&#x5B9A;&#x8FD9;&#x4E48;&#x505A;&#xFF0C;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x4E1A;&#x52A1;&#x7684;&#x5177;&#x4F53;&#x573A;&#x666F;&#x6765;&#x5212;&#x5206;&#xFF0C;&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x4E1A;&#x52A1;&#x573A;&#x666F;&#xFF1A;</p>
<ul>
<li>&#x670D;&#x52A1;&#x76EE;&#x524D;QPS10&#x4E07;&#xFF0C;&#x9884;&#x8BA1;&#x51E0;&#x5E74;&#x4E4B;&#x5185;&#x4F1A;&#x53D1;&#x5C55;&#x5230;&#x767E;&#x4E07;&#x3002;</li>
<li>&#x5F53;&#x524D;&#x673A;&#x5668;&#x4E09;&#x5730;&#x90E8;&#x7F72;&#xFF0C;&#x4E0A;&#x6D77;&#xFF0C;&#x5317;&#x4EAC;&#xFF0C;&#x6DF1;&#x5733;&#x90FD;&#x6709;&#x3002;</li>
<li>&#x5F53;&#x524D;&#x673A;&#x5668;10&#x53F0;&#x5DE6;&#x53F3;&#xFF0C;&#x9884;&#x8BA1;&#x672A;&#x6765;&#x4F1A;&#x589E;&#x52A0;&#x81F3;&#x767E;&#x53F0;&#x3002;</li>
</ul>
<p>&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x6839;&#x636E;&#x4E0A;&#x9762;&#x7684;&#x573A;&#x666F;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x5408;&#x7406;&#x7684;&#x5212;&#x5206;62bit,QPS&#x51E0;&#x5E74;&#x4E4B;&#x5185;&#x4F1A;&#x53D1;&#x5C55;&#x5230;&#x767E;&#x4E07;&#xFF0C;&#x90A3;&#x4E48;&#x6BCF;&#x6BEB;&#x79D2;&#x5C31;&#x662F;&#x5343;&#x7EA7;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x76EE;&#x524D;10&#x53F0;&#x673A;&#x5668;&#x90A3;&#x4E48;&#x6BCF;&#x53F0;&#x673A;&#x5668;&#x627F;&#x62C5;&#x767E;&#x7EA7;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x6269;&#x5C55;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x5FAA;&#x73AF;&#x4F4D;&#x53EF;&#x4EE5;&#x9650;&#x5236;&#x5230;1024&#xFF0C;&#x4E5F;&#x5C31;&#x662F;2^10&#xFF0C;&#x90A3;&#x4E48;&#x5FAA;&#x73AF;&#x4F4D;10&#x4F4D;&#x5C31;&#x8DB3;&#x591F;&#x4E86;&#x3002;</p>
<p>&#x673A;&#x5668;&#x4E09;&#x5730;&#x90E8;&#x7F72;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;3bit&#x603B;&#x5171;8&#x6765;&#x8868;&#x793A;&#x673A;&#x623F;&#x4F4D;&#x7F6E;&#xFF0C;&#x5F53;&#x524D;&#x7684;&#x673A;&#x5668;10&#x53F0;&#xFF0C;&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x6269;&#x5C55;&#x5230;&#x767E;&#x53F0;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x7528;7bit 128&#x6765;&#x8868;&#x793A;&#xFF0C;&#x65F6;&#x95F4;&#x4F4D;&#x4F9D;&#x7136;&#x662F;41bit,&#x90A3;&#x4E48;&#x8FD8;&#x5269;&#x4E0B;64-10-3-7-41-1 = 2bit,&#x8FD8;&#x5269;&#x4E0B;2bit&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#x3002;</p>
<p><img src="../images/snowflake2.png" alt="snowflake"></p>
</li>
</ul>
<p><strong>&#x65F6;&#x949F;&#x56DE;&#x62E8;</strong></p>
<p>&#x56E0;&#x4E3A;&#x673A;&#x5668;&#x7684;&#x539F;&#x56E0;&#x4F1A;&#x53D1;&#x751F;&#x65F6;&#x95F4;&#x56DE;&#x62E8;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x96EA;&#x82B1;&#x7B97;&#x6CD5;&#x662F;&#x5F3A;&#x4F9D;&#x8D56;&#x6211;&#x4EEC;&#x7684;&#x65F6;&#x95F4;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x65F6;&#x95F4;&#x53D1;&#x751F;&#x56DE;&#x62E8;&#xFF0C;&#x6709;&#x53EF;&#x80FD;&#x4F1A;&#x751F;&#x6210;&#x91CD;&#x590D;&#x7684;ID&#xFF0C;&#x5728;&#x6211;&#x4EEC;&#x4E0A;&#x9762;&#x7684;nextId&#x4E2D;&#x6211;&#x4EEC;&#x7528;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x548C;&#x4E0A;&#x4E00;&#x6B21;&#x7684;&#x65F6;&#x95F4;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x5C0F;&#x4E8E;&#x4E0A;&#x4E00;&#x6B21;&#x7684;&#x65F6;&#x95F4;&#x90A3;&#x4E48;&#x80AF;&#x5B9A;&#x662F;&#x53D1;&#x751F;&#x4E86;&#x56DE;&#x62E8;&#xFF0C;&#x7B97;&#x6CD5;&#x4F1A;&#x76F4;&#x63A5;&#x629B;&#x51FA;&#x5F02;&#x5E38;.</p>
<h2 id="2-&#x9ED1;&#x9A6C;&#x5934;&#x6761;">2 &#x9ED1;&#x9A6C;&#x5934;&#x6761;</h2>
<h5 id="&#x4F7F;&#x7528;&#x96EA;&#x82B1;&#x7B97;&#x6CD5;-&#xFF08;&#x4EE3;&#x7801;-toutiaobackendcommonutilssnowflake">&#x4F7F;&#x7528;&#x96EA;&#x82B1;&#x7B97;&#x6CD5; &#xFF08;&#x4EE3;&#x7801; toutiao-backend/common/utils/snowflake)</h5>
<pre><code class="lang-python"><span class="hljs-comment"># Twitter&apos;s Snowflake algorithm implementation which is used to generate distributed IDs.</span>
<span class="hljs-comment"># https://github.com/twitter-archive/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala</span>

<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> logging

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidSystemClock</span><span class="hljs-params">(Exception)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x65F6;&#x949F;&#x56DE;&#x62E8;&#x5F02;&#x5E38;
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-comment"># 64&#x4F4D;ID&#x7684;&#x5212;&#x5206;</span>
WORKER_ID_BITS = <span class="hljs-number">5</span>
DATACENTER_ID_BITS = <span class="hljs-number">5</span>
SEQUENCE_BITS = <span class="hljs-number">12</span>

<span class="hljs-comment"># &#x6700;&#x5927;&#x53D6;&#x503C;&#x8BA1;&#x7B97;</span>
MAX_WORKER_ID = -<span class="hljs-number">1</span> ^ (-<span class="hljs-number">1</span> &lt;&lt; WORKER_ID_BITS)  <span class="hljs-comment"># 2**5-1 0b11111</span>
MAX_DATACENTER_ID = -<span class="hljs-number">1</span> ^ (-<span class="hljs-number">1</span> &lt;&lt; DATACENTER_ID_BITS)

<span class="hljs-comment"># &#x79FB;&#x4F4D;&#x504F;&#x79FB;&#x8BA1;&#x7B97;</span>
WOKER_ID_SHIFT = SEQUENCE_BITS
DATACENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS
TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATACENTER_ID_BITS

<span class="hljs-comment"># &#x5E8F;&#x53F7;&#x5FAA;&#x73AF;&#x63A9;&#x7801;</span>
SEQUENCE_MASK = -<span class="hljs-number">1</span> ^ (-<span class="hljs-number">1</span> &lt;&lt; SEQUENCE_BITS)

<span class="hljs-comment"># Twitter&#x5143;&#x5E74;&#x65F6;&#x95F4;&#x6233;</span>
TWEPOCH = <span class="hljs-number">1288834974657</span>


logger = logging.getLogger(<span class="hljs-string">&apos;flask.app&apos;</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IdWorker</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x7528;&#x4E8E;&#x751F;&#x6210;IDs
    &quot;&quot;&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, datacenter_id, worker_id, sequence=<span class="hljs-number">0</span>)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x521D;&#x59CB;&#x5316;
        :param datacenter_id: &#x6570;&#x636E;&#x4E2D;&#x5FC3;&#xFF08;&#x673A;&#x5668;&#x533A;&#x57DF;&#xFF09;ID
        :param worker_id: &#x673A;&#x5668;ID
        :param sequence: &#x5176;&#x5B9E;&#x5E8F;&#x53F7;
        &quot;&quot;&quot;</span>
        <span class="hljs-comment"># sanity check</span>
        <span class="hljs-keyword">if</span> worker_id &gt; MAX_WORKER_ID <span class="hljs-keyword">or</span> worker_id &lt; <span class="hljs-number">0</span>:
            <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&apos;worker_id&#x503C;&#x8D8A;&#x754C;&apos;</span>)

        <span class="hljs-keyword">if</span> datacenter_id &gt; MAX_DATACENTER_ID <span class="hljs-keyword">or</span> datacenter_id &lt; <span class="hljs-number">0</span>:
            <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&apos;datacenter_id&#x503C;&#x8D8A;&#x754C;&apos;</span>)

        self.worker_id = worker_id
        self.datacenter_id = datacenter_id
        self.sequence = sequence

        self.last_timestamp = -<span class="hljs-number">1</span>  <span class="hljs-comment"># &#x4E0A;&#x6B21;&#x8BA1;&#x7B97;&#x7684;&#x65F6;&#x95F4;&#x6233;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_gen_timestamp</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x751F;&#x6210;&#x6574;&#x6570;&#x65F6;&#x95F4;&#x6233;
        :return:int timestamp
        &quot;&quot;&quot;</span>
        <span class="hljs-keyword">return</span> int(time.time() * <span class="hljs-number">1000</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_id</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;&#x65B0;ID
        :return:
        &quot;&quot;&quot;</span>
        timestamp = self._gen_timestamp()

        <span class="hljs-comment"># &#x65F6;&#x949F;&#x56DE;&#x62E8;</span>
        <span class="hljs-keyword">if</span> timestamp &lt; self.last_timestamp:
            logging.error(<span class="hljs-string">&apos;clock is moving backwards. Rejecting requests until {}&apos;</span>.format(self.last_timestamp))
            <span class="hljs-keyword">raise</span> InvalidSystemClock

        <span class="hljs-keyword">if</span> timestamp == self.last_timestamp:
            self.sequence = (self.sequence + <span class="hljs-number">1</span>) &amp; SEQUENCE_MASK
            <span class="hljs-keyword">if</span> self.sequence == <span class="hljs-number">0</span>:
                timestamp = self._til_next_millis(self.last_timestamp)
        <span class="hljs-keyword">else</span>:
            self.sequence = <span class="hljs-number">0</span>

        self.last_timestamp = timestamp

        new_id = ((timestamp - TWEPOCH) &lt;&lt; TIMESTAMP_LEFT_SHIFT) | (self.datacenter_id &lt;&lt; DATACENTER_ID_SHIFT) | \
                 (self.worker_id &lt;&lt; WOKER_ID_SHIFT) | self.sequence
        <span class="hljs-keyword">return</span> new_id

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_til_next_millis</span><span class="hljs-params">(self, last_timestamp)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x7B49;&#x5230;&#x4E0B;&#x4E00;&#x6BEB;&#x79D2;
        &quot;&quot;&quot;</span>
        timestamp = self._gen_timestamp()
        <span class="hljs-keyword">while</span> timestamp &lt;= last_timestamp:
            timestamp = self._gen_timestamp()
        <span class="hljs-keyword">return</span> timestamp


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    worker = IdWorker(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>)
    print(worker.get_id())
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C02-Database/DatabaseTheory.html" class="navigation navigation-prev " aria-label="Previous page: 数据库理论"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C02-Database/DatabaseOptimize.html" class="navigation navigation-next " aria-label="Next page: 数据库优化"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
